<?php

//Load Master Page
require_once('../shared/layout.php');

//Load Shared Objects & Controls
require_once('../shared/alert.php');

class Edit extends Layout {

    protected $objAlert;
    protected $mctProject;
    protected $txtName, $txtDescription, $calDate, $chkIsEnable, $ddlService;
    protected $btnSave;

    protected function Form_Create() {
        parent::Form_Create();

        $this->strContextMenu = __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/project/menu/edit.tpl.php';

        $this->CssClass = 'form-horizontal';

        $this->objAlert = new Alert($this);

        $this->mctProject = ProjectMetaControl::CreateFromQueryString($this, QMetaControlCreateType::CreateOrEdit);

        $this->txtName = $this->mctProject->txtName_Create();
        $this->txtDescription = $this->mctProject->txtDescription_Create();
        $this->calDate = $this->mctProject->calDate_Create();

        $this->chkIsEnable = $this->mctProject->chkIsEnable_Create();
        $this->chkIsEnable->AddCssFile('../../../admin/content/plugins/jquery-prettyCheckable/dist/prettyCheckable.css');
        $this->chkIsEnable->AddJavascriptFile('../../../admin/content/plugins/jquery-prettyCheckable/dist/prettyCheckable.min.js');

        $this->ddlService = $this->mctProject->ddlService_Create();
        $this->ddlService->SetCustomAttribute('title', 'Select services...');
        $this->ddlService->SetCustomAttribute('data-style', 'btn-info');
        $this->ddlService->SetCustomAttribute('data-width', '100%');
        $this->ddlService->SetCustomAttribute('data-size', '8');
        $this->ddlService->SetCustomAttribute('data-selected-text-format', 'count>3');

        $objServices = Service::QueryArray(
                        QQ::Equal(QQN::Service()->IsEnable, true), QQ::Clause(QQ::OrderBy(QQN::Service()->Name)));

        if ($objServices)
            foreach ($objServices as $objService)
                $this->ddlService->AddItem($objService->Name, $objService);

        $this->ddlService->SelectedValues = Service::QueryArray(
                        QQ::AndCondition(
                                QQ::Equal(QQN::Service()->ProjectService->ProjectId, $this->mctProject->Project->Id), QQ::Equal(QQN::Service()->IsEnable, true)));

        $this->ddlService->AddCssFile('../../../admin/content/plugins/bootstrap-select/bootstrap-select.min.css');
        $this->ddlService->AddJavascriptFile('../../../admin/content/plugins/bootstrap-select/bootstrap-select.min.js');

        $this->btnSave = new QButton($this);
        $this->btnSave->Text = QApplication::Translate('Save Changes');
        $this->btnSave->CausesValidation = $this->btnSave->PrimaryButton = true;
        $this->btnSave->AddAction(new QClickEvent(), new QToggleEnableAction($this->btnSave));
        $this->btnSave->AddAction(new QClickEvent(), new QAjaxAction('btnSave_Click'));

        $this->btnSave->AddJavascriptFile('../../../admin/content/js/project/edit.js');
    }

    protected function Form_PreRender() {
        parent::Form_PreRender();

        QApplication::ExecuteJavaScript('Edit.init();');
    }

    protected function Form_Validate() {
        $blnToReturn = parent::Form_Validate();

        if ($blnToReturn)
            $this->btnSave->Enabled = true;

        return $blnToReturn;
    }
    
    protected function btnSave_Click($strFormId, $strControlId, $strParameter) {
        try {
            if (!$this->mctProject->EditMode)
                $this->mctProject->Project->LogInsert = QDateTime::Now();

            $this->mctProject->Project->LogUser = $this->objCurrentUser->Username;
            $this->mctProject->SaveProject();

            $this->mctProject->Project->DeleteAllProjectServices();

            $objSelected = $this->ddlService->SelectedValues;
            foreach ($objSelected as $objService) {
                $objProjectService = new ProjectService();
                $objProjectService->Project = $this->mctProject->Project;
                $objProjectService->Service = $objService;
                $objProjectService->LogInsert = $this->mctProject->Project->LogInsert;
                $objProjectService->LogUser = $this->mctProject->Project->LogUser;
                $objProjectService->Save();
            }

            $this->objAlert->Success(QApplication::Translate('Success'), QApplication::Translate('Project Saved Successful !'));
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            $this->objAlert->Error(QApplication::Translate('Ooops!'), $objExc->getMessage());
        }

        $this->btnSave->Enabled = true;
    }

}

Edit::Run('Edit', __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/project/edit.tpl.php');
?>